<?php
/**
 * Model for eqtech-hotelbooking Component
 * 
 * @package    eqtech-hotelbooking joomla
 * @subpackage Components
 * @link 
 * @license        GNU/GPL
 */
 
// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport('joomla.application.component.controller');
/**
 *
 * @package    eqtech-hotelbooking joomla
 * @subpackage Components
 */
class HbControllerCrud extends JController
{
	//assign by subclass
	protected $com_name;
	protected $view_name;
	protected $model_name;
	
	/**
	 * constructor (registers additional tasks to methods)
	 * @return void
	 */
	function __construct()
	{
		parent::__construct();
		$this->registerTask( 'add'  , 	'edit' );
	}
 
	/**
	 * display the edit form
	 * @return void
	 */
	 
	function getViewClassName($view_name){
		if(!$view_name)
		{
			throw new Exception('view_name can not be null');
		}
		return $this->com_name.'View'.$view_name;
	}
	
	function getModelClassName($model_name){
		if(!$model_name)
		{
			throw new Exception('model_name can not be null');
		}
		return $this->com_name.'Model'.$model_name;
	}
	
	function getViewClassPath($view_name){
		if(!$view_name)
		{
			throw new Exception('view_name can not be null');
		}
		return JPATH_COMPONENT.DS.'views'.DS. strtolower($view_name).DS.'view.html.php';
	}

	function getModelClassPath($model_name){
		if(!$model_name)
		{
			throw new Exception('model_name can not be null');
		}
		return JPATH_COMPONENT.DS.'models'.DS. strtolower($model_name).'.php';
	}
	
	/**
	 * get a model Instance by a give model name (same as getModel('model_name') ),
	 may used by subclass to get any view instance
	 * @return void
	 */
	function getModelInstance($model_name)
	{
		$model_class_name=$this->getModelClassName($model_name);	
		$model_class_path=$this->getModelClassPath($model_name);
		include_once($model_class_path);
		$model_instance = new $model_class_name(); 
		return $model_instance;
	}	
	
	/**
	 * get any view Instance by a give view name, may used by subclass to get any view instance
	 * @return void
	 */
	function getViewInstance($view_name)
	{	
		$view_class_name=$this->getViewClassName($view_name);
		$view_class_path=$this->getViewClassPath($view_name);
		include($view_class_path);
		$view_instance = new $view_class_name();				
		return $view_instance;
	}
	
	/**
	 * display a record
	 * @return void
	 */
	function edit()
	{

		$view = & $this->getView();
		$view->displayItem();
	}
	
	/**
	 * display record list
	 * @return void
	 */
	function editList()
	{
		$view = & $this->getView('country','html');
		//$model=$this->getModel($this->model_name);
		//$model=$this->getModelInstance($this->model_name);
		//$view->setModel($model,true);
		$view->displayItemList();
		
		
	}
	
	/**
	 * save a record (and redirect to main page)
	 * @return void
	 */
	function save()
	{
		$model = $this->getModel($this->model_name);
		
		if ($model->store()) {
			$msg = JText::_( 'Country Record Saved!' );
		} else {
			$msg = JText::_( 'Error Saving Country Record' );
		}
		$link = 'index.php?option=com_hb&view=countries';
		$this->setRedirect($link, $msg);
		
	}
	
	
	
	/**
	 * remove record(s)
	 * @return void
	 */
	function remove()
	{
		$model = $this->getModel($this->model_name);
		if(!$model->delete()) {
			$msg = JText::_( 'Error: One or More Country Record Could not be Deleted' );
		} else {
			$msg = JText::_( 'Country Record(s) Deleted' );
		}
	 $link = 'index.php?option=com_hb&view=countries';
			$this->setRedirect($link, $msg);
	}

}
